import { Item } from '../../object/Item';
import { ItemType, ItemShowType } from '../../define/GameDefine';
import ViewMgr from '../../manager/ViewMgr';
import { UICommandKey } from '../../define/UICommand';
import { tool } from '../../tool/UITool';

const { ccclass, property } = cc._decorator;

@ccclass
export default class ItemUI extends cc.Component {
  @property(cc.Node)
  highLight: cc.Node = null;

  @property(cc.Sprite)
  itemImg: cc.Sprite = null;

  @property(cc.Label)
  itemNum: cc.Label = null;

  // LIFE-CYCLE CALLBACKS:

  onLoad() {
    this.node.on(cc.Node.EventType.TOUCH_END, this.onClickCard, this);
  }

  private onClickCard(): void {
    switch (this._showType) {
      case ItemShowType.BattleCardUse:
        ViewMgr.ins.emit(UICommandKey.onClickItemInBattle, { target: this.item.hid });
        break;
    }
  }

  start() {}

  public showHighLight(): void {
    this.highLight.active = true;
  }

  public hideHighLight(): void {
    this.highLight.active = false;
  }

  public item: Item;
  public renderItem(item: Item): void {
    this.item = item;
    this.hideHighLight();
    this.renderIcon();
    this.renderNum();
  }

  public renderIcon(): void {
    if (this.item.icon) {
      this.itemImg.node.active = true;
      let imgSource: string = `items/${this.item.icon}`;
      tool.setSource(this.itemImg, imgSource);
    } else {
      this.itemImg.node.active = false;
    }
  }

  public renderNum(): void {
    if (this.item.max == 1) {
      this.itemNum.node.active = false;
    } else {
      this.itemNum.node.active = true;
      this.itemNum.string = `x${this.item.count}`;
    }
  }

  private _showType: ItemShowType;
  public set itemShowType(type: ItemShowType) {
    this._showType = type;
  }

  // update (dt) {}
}
